package com.softlab.retailwholesalesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softlab.retailwholesalesystem.entity.VO.RepositoryGoodsVOEntity;
import com.softlab.retailwholesalesystem.entity.StoreEntity;
import com.softlab.retailwholesalesystem.mapper.GoodsMapper;
import com.softlab.retailwholesalesystem.mapper.StoreMapper;
import com.softlab.retailwholesalesystem.service.IStoreService;
import com.softlab.retailwholesalesystem.service.exception.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, StoreEntity> implements IStoreService{

    @Autowired
    StoreMapper mapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public List<RepositoryGoodsVOEntity> getAllStore() {
        QueryWrapper<StoreEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("u.is_delete",0);
        List<RepositoryGoodsVOEntity> VOList = mapper.selectListByQuery(wrapper);

        for (RepositoryGoodsVOEntity VO : VOList){
            VO.setMoney(goodsMapper.selectById(VO.getGoodsId()).getStockPrice() * VO.getGoodsNumber());
        }

        return VOList;
    }

    @Override
    public void addStore(StoreEntity store) {
        QueryWrapper<StoreEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",store.getGoodsId());
        wrapper.eq("repository_id",store.getRepositoryId());
        wrapper.eq("is_delete",0);
        if (mapper.selectOne(wrapper) == null){
            int i = mapper.insert(store);
            if(i == 0)
                throw new InsertRunTimeException("插入时错误");
        }
        else
            throw new StoreNameDuplicatedException("该仓库该货品已存在");
    }

    @Override
    public void deleteStore(StoreEntity store) {
        store.setIsDelete(1);
        int i = mapper.updateById(store);
        if(i == 0){
            throw new DeleteErrorException("删除时遇到未知错误");
        }
    }

    @Override
    public void changeStore(StoreEntity store) {
        int i = mapper.updateById(store);
        if (i == 0){
            throw new UpdateErrorException("新增时出现异常");
        }
    }

    @Override
    public StoreEntity getStore(StoreEntity store) {
        QueryWrapper<StoreEntity> wrapper = new QueryWrapper<>();
        if ((store.getGoodsId() != null) && (store.getRepositoryId() != null)) {
            wrapper.eq("goods_id", store.getGoodsId());
            wrapper.eq("repository_id", store.getRepositoryId());
        }
        if (store.getStoreId() != null)
            wrapper.eq("store_id", store.getGoodsId());
        return mapper.selectOne(wrapper);
    }

    @Override
    public List<RepositoryGoodsVOEntity> getStores(StoreEntity store) {
        QueryWrapper<StoreEntity> wrapper = new QueryWrapper<>();

        if (store.getStoreId()!=null) wrapper.eq("u.store_id", store.getStoreId());
        if (store.getGoodsId() != null) wrapper.eq("u.goods_id", store.getGoodsId());
        if (store.getGoodsNumber() != null) wrapper.eq("u.goods_number", store.getGoodsNumber());
        if (store.getRepositoryId() != null) wrapper.eq("u.repository_id", store.getRepositoryId());
        wrapper.eq("u.is_delete",0);


        List<RepositoryGoodsVOEntity> VOList = mapper.selectListByQuery(wrapper);

        for (RepositoryGoodsVOEntity VO : VOList){
            VO.setMoney(goodsMapper.selectById(VO.getGoodsId()).getStockPrice() * VO.getGoodsNumber());
        }

        return VOList;
    }
}
